cmake_minimum_required(VERSION 3.16)
project(ChatServerMock LANGUAGES CXX)

set(PCH_HEADERS
    <QtWidgets>           # 包含所有常用 Qt Widget 类（推荐用于 PCH）
    <QtGui>               # 如 QPixmap, QPainter, QIcon 等
    <QtCore>              # QString, QList, QObject 等
    <QtNetwork>
)

find_package(Qt6 6.5 REQUIRED COMPONENTS Core Widgets Protobuf HttpServer WebSockets)

file(GLOB PB_FILES "../ChatClient//proto/*.proto")

qt_standard_project_setup()

qt_add_executable(ChatServerMock
    WIN32 MACOSX_BUNDLE
    main.cpp
    widget.cpp
    widget.h
    widget.ui
    server.h
    server.cpp
    resource.qrc
    resource/file/test.txt
)

# 在 qt_add_executable 之后添加手动资源编译
find_program(RCC_EXECUTABLE NAMES rcc6 rcc PATHS ${Qt6_DIR}/../../../bin)

if(RCC_EXECUTABLE)
    message(STATUS "找到 rcc 工具: ${RCC_EXECUTABLE}")

    # 手动编译资源
    add_custom_command(
        OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/qrc_resources.cpp
        COMMAND ${RCC_EXECUTABLE}
        ARGS -name resources ${CMAKE_CURRENT_SOURCE_DIR}/resource.qrc -o ${CMAKE_CURRENT_BINARY_DIR}/qrc_resources.cpp
        DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/resource.qrc
        WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
        VERBATIM
    )

    # 将生成的资源文件添加到目标
    target_sources(ChatServerMock PRIVATE ${CMAKE_CURRENT_BINARY_DIR}/qrc_resources.cpp)
    message(STATUS "手动资源编译已配置")
else()
    message(WARNING "未找到 rcc 工具，资源编译可能失败")
endif()


target_precompile_headers(ChatServerMock PRIVATE ${PCH_HEADERS})
qt_add_protobuf(ChatServerMock PROTO_FILES ${PB_FILES})

target_link_libraries(ChatServerMock
    PRIVATE
        Qt::Core
        Qt::Widgets
        Qt6::HttpServer
        Qt6::WebSockets
        Qt6::Protobuf
)


include(GNUInstallDirs)

install(TARGETS ChatServerMock
    BUNDLE  DESTINATION .
    RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
    LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
)

qt_generate_deploy_app_script(
    TARGET ChatServerMock
    OUTPUT_SCRIPT deploy_script
    NO_UNSUPPORTED_PLATFORM_ERROR
)
install(SCRIPT ${deploy_script})
